<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Farmacias
 *
 * @author marcelo.diaz
 */
class Farmacias extends Controller{
	function Farmacias(){
		parent::Controller("farmacias");
		$this->load->model("Farmacias_Model");
	}

  
	function deturno(){
		$json = $this->Farmacias_Model->get_deturno();
		header('Content-Type:application/json');
		//!!!
		echo json_encode($json);
	}
	
	function index()
	{
		$email = "ALL";
		$this->email->from('conocepunta@gmail.com');
		$this->email->to('conocepunta@gmail.com');
		$this->email->subject('Farmacias access');
	
		$data['page_title'] = "Farmacias en Punta Alta - Farmacias de Turno";
		$data['meta_description'] = '<meta name="description" content="ConocePunta - Punta Alta, Buenos Aires, Argentina - Farmacias en Punta Alta - Farmacias de Turno" />';
		$output = $this->load->view('head_view', $data, true);
	
		$places = $this->Farmacias_Model->_all();
	
		$navbar = $this->write_navbar();
		//iterate and parse places
		$output .= $navbar;
		$output .= "<div class='content centered'>";
		$output .= 	$this->write_custom_content($places);
		$output .= 	$this->write_sidebar("farmacias");
		$output .= "</div>";
		//---------MAP---------
		$output .= $this->write_map();
		$output .= $this->load->view('foot_view', "", true);
		$this->output->set_output($output);
		//$this->email->message($email);
		//$this->email->send();
	}
	
	function name($place){
		$json = $this->Farmacias_Model->get_place($place);
		header('Content-Type:application/json');
		echo json_encode($json);
	}
	
	function all(){
		$json = $this->Farmacias_Model->get_all();
		header('Content-Type:application/json');
		//!!!
		echo json_encode($json);
	}
	
	function nombre($name = false){
		if ($name){
			$email = "[".$name."]";
			$this->email->from('conocepunta@gmail.com');
			$this->email->to('conocepunta@gmail.com');
			$this->email->subject('[Farmacias] access');
	
			$name_replaced = str_replace("-", " ", $name);
			$navbar = $this->write_navbar();
			$data['page_title'] = $name_replaced." - Farmacias en Punta Alta - Farmacias de Turno";
			$data['meta_description'] = '<meta name="description" content="'.$name_replaced.' - ConocePunta - Punta Alta, Buenos Aires, Argentina - Farmacias en Punta Alta - Farmacias de Turno" />';
			$output = $this->load->view('head_view', $data, true);
	
			$banner_adsense = '<div class="lheader"><script type="text/javascript"><!--
			google_ad_client = "pub-2113662635661393";
	
			google_ad_slot = "4418063429";
			google_ad_width = 728;
			google_ad_height = 90;
			//-->
			</script>
			<script type="text/javascript"
			src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script></div>';
	
			$output .= $navbar.
			"<div class='content slim-content centered' style='height:120px'>".
			$banner_adsense.
			"</div>";
	
			$content = "";
			//$place = $this->Negocios_Model->_place(strtolower($name));
			$place = $this->Farmacias_Model->get_place($name);
			if (!$place){
				$places = $this->Farmacias_Model->_all();
				//iterate and parse places
				$content .= "<div class='content centered'>".
						"<p class='ui-state-highlight' style='width:50%; padding:10px; text-align:center;'>No hemos podido encontrar <strong>$name</strong>.</p>";
				$content .= 	$this->write_custom_content($places);
				$content .= 	$this->write_sidebar("farmacias");
				$content .= "</div>";
				$email .= "NOT FOUND";
			} else{
				//parse place
				$content .= "<div class='content slim-content centered'>".
						$this->write_specific_item($place).
						"</div>";
			}
	
			//$this->email->message($email);
			//$this->email->send();			
			$output .= $content;		 
	
			$output .= $this->load->view('foot_view', "", true);
			$this->output->set_output($output);
		}else{
			$this->index();
		}
	}

	/**
	 * (non-PHPdoc)
	 * @see Controller::write_specific_item()
	 */
	function write_specific_item($marker){
		$content = "";
		$marker_name = str_replace("-", " ", $marker['name']);
		$url  = "www.conocepunta.com.ar/".$this->get_controller_name()."/nombre/".$marker['name'];
			
		//social widgets---------------------
		$fb   = "<div class='fb_like'><iframe src='http://www.facebook.com/plugins/like.php?href=".$url."&amp;layout=button_count&amp;show-faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;locale=es_LA' frameborder='0' scrolling='no' style='vertical-align: middle; border: medium none; overflow: hidden; width: 100px; height: 21px;' allowtransparency='true'></iframe></div>";
		$addthis = '<div class="addthis_toolbox fb_like">'.
				'<div class="hover_effect">'.
				'<div style="display:inline-block"><a class="addthis_button_twitter"'.
				'addthis:url="http://'.$url.'"'.
				'addthis:title="'.$marker_name.' en '.$marker["address"].' #'.str_replace(" ","", $marker["city"]).' @conocepunta">&nbsp;</a></div>'.
				'<div style="display:inline-block"><a class="addthis_button_google"'.
				'addthis:url="http://'.$url.'"'.
				'addthis:title="'.$marker_name.': '.$marker["address"].' @conocepunta">&nbsp;</a></div>'.
				'<div style="display:inline-block"><a class="addthis_button_expanded">&nbsp;&nbsp;Mas</a></div>'.
				'<div style="clear:both; float:none;"></div>'.
				'</div>'.
				'</div>';
			
		$social = "<div>".$fb.$addthis."</div>";
		//--------------------------------
		//$image = "<figure><img alt='".$marker_name."' class='lheader' src='".$marker['img']."'></figure>";
		//---------------static map
		//$image = "<figure><img alt='".$marker_name."' class='lheader' src='http://maps.google.com/staticmap?center=".$marker['lat'].",".$marker['lng']."&zoom=15&size=225x225&maptype=satellite&&markers=icon:".$marker['icon']."%7C".$marker['lat'].",".$marker['lng']."&key=ABQIAAAALyKwFZzMQBTWU3CUmBrN7RTvufBQ4WA-npmjEqhq0GDcUkCFMRTabPeW_m0vIN3Bghd4m7Mv_m-mOQ'></figure>".
		$image = "<figure><img alt='".$marker_name."' class='lheader' src='http://maps.google.com/staticmap?center=".$marker['lat'].",".$marker['lng']."&zoom=15&size=225x225&maptype=satellite&&markers=".$marker['lat'].",".$marker['lng']."&key=ABQIAAAALyKwFZzMQBTWU3CUmBrN7RTvufBQ4WA-npmjEqhq0GDcUkCFMRTabPeW_m0vIN3Bghd4m7Mv_m-mOQ'></figure>";
		//-------------------------
	
		$desc = "<p>".$marker['description']."</p>";
		//$screen = "<div class='screenshot vintage lheader' style='margin-bottom:7px'>".
		$screen = "<div class='vintage lheader' style='margin-bottom:7px'>".
				$image.
				"</div>".
				"<div class='figcaption'>".
				//str_pad(substr($marker_name,0,15),18,"...").
		//$marker_name.
		"<p>".$marker['address']."</p>".
		"<p class='place'>".$marker['city']."</p>".
		"</div>".
		$social;
		//----------------------------------------------------------------------
		//loading related data
		$related = "<ul>";
		foreach ($marker['related'] as $k => $rel){
			$clean_name = str_replace("-", " ", $rel['name']);
			$related .= "<li class='box mini-post figcaption'>
			<figure><img class='small' title='".$clean_name."' alt='".$clean_name."' src='".$rel['img']."'></figure>
			<a href='".base_url().$this->category_string($rel['marker_cat'])."/nombre/".$rel['name']."' target='_new'>".$clean_name."</a>
			</li>";
		}
		$related .= "</ul>";
	
		//-------------------------------------------------------------------------	
		$query = $this->db->query("SELECT *
				FROM deturno
				WHERE deturno.farmacia = ".$marker['id_marker']." AND
				(NOW() BETWEEN deturno.fecha_inicio AND deturno.fecha_fin)
				");
		$deturno = $query->row_array();		
		$deturno_html = (empty($deturno))?"":"<p class='comment' style='color:#049B02;' title='De Turno'>AHORA de Turno</p>";
		//-------------------------------------------------------------------------
		//--ADMINISTRAR SECTION
		$administrar = "";
		//si no tiene row en socio-marker ==> mostrar span con redireccion a /socios
		$query = $this->db->query("SELECT *
									FROM marker as m
									LEFT outer join socio_marker as rel
									on m.id_marker=rel.id_marker
									WHERE rel.id_marker IS NULL and
										  m.id_marker = ".$marker['id_marker']
								);
		$marker_asociado = $query->row_array();
		if(!empty($marker_asociado)){		
			$attributes = array('class' => ' rheader ');
			$administrar .= form_open('socios', $attributes).
								form_hidden('id_marker',$marker['id_marker']).
								form_hidden('marker_name',$marker['name']).
								form_submit('submit', 'Administrar', 'class="manage submit" title="Es tu Farmacia?"').
							form_close();
		}else{
			//esta ASOCIADO
			//Determinar el plan y cargar la informacion correspondiente
		}
		//---------------------------------------------------------------------------
		$content =  "<div class='brief box post'>".
				"<div>".
					"<h1 class='inline'><p>".$marker_name."</p> ".$deturno_html."</h1>".
					$administrar.
				"</div>".
				"<p class='separator'></p>".
				//------
				"<article id='".$marker['name']."' class='inline box post lheader'>".
					$screen.
				"</article>".
				"<article class='inline' style='padding-left: 20px; max-width: 500px;'>".
					"<div>".$desc."</div>".					
				"</article>".
				"<article class='inline figcaption' style='width:100%'>".
					"<p class='separator'></p>".
					"<p class='place' style='text-align:left; padding-bottom:10px'>Entradas relacionadas...</p>".
					$related.
				"</article>".
			"</div>";
		return $content;
	}

	function write_custom_content($places){
		$content = "<div id='events' class='lheader'>";
		$counter = 1;
	
		foreach ($places as $place){
			//-------------------------------------------------------------------------
			//DE TURNO
			$query = $this->db->query("SELECT *
										FROM deturno
										WHERE deturno.farmacia = ".$place['id_marker']." AND
										(NOW() BETWEEN deturno.fecha_inicio AND deturno.fecha_fin)
										");
			$deturno = $query->row_array();
			//$deturno_tape = (empty($deturno))?"":"<div class='tape'><div class='triangle triangle_left_r'></div><span class='date deturno inline'>De Turno</span></div>";
			$deturno_css = (empty($deturno))?"":" deturno ";
			$deturno_title = (empty($deturno))?"":" - AHORA DE TURNO";
			//-------------------------------------------------------------------------
					
			//if(($counter % 15) != 0){
				$face_like = '<span>
				<iframe src="http://www.facebook.com/plugins/like.php?href='.base_url().$this->get_controller_name().'/nombre/'.$place['name'].'&amp;layout=button_count&amp;show-faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;locale=es_LA" frameborder="0" scrolling="no" style="vertical-align: middle; border: medium none; overflow: hidden; width: 100px; height: 21px;" allowtransparency="true"></iframe>
				</span>';
				$name_replaced = str_replace("-", " ", $place['name']);
					
				$image = "<figure><img title='".$name_replaced."' alt='".$name_replaced."' src='".$place['img']."'></figure>";
				$content .= "<article class='inline box post $deturno_css' title='".$name_replaced." ".$deturno_title."'>".
							//$deturno_tape.
							"<div class='screenshot vintage'>".
								$image.
								"<p class='hover'>".
								"<a class='hoverButton inline' href='".base_url().$this->get_controller_name()."/nombre/".$place['name']."'>Mas</a>".
								//"<a class='hoverButton inline geoButton' id='".$place['name']."' onclick='locate(\"".$this->get_controller_name()."\", this.id);'>Mapa</a>".	NO MORE MAP BUTTON
								"</p>".
							"</div>".
							"<div class='figcaption'>"."<p>$name_replaced</p>"."</div>".
							"<div class='fb_like'>$face_like</div>".
						"</article>";
			//}else{
			if(($counter % 10) == 0){
					//post Google Ad
					$content .= "<article class='inline box post'>".
						"<div class='screenshot vintage'>".
						"<figure>".
						"<script type='text/javascript'><!--
						google_ad_client = 'ca-pub-2113662635661393';
						/* custom_content */
						google_ad_slot = '7944803694';
						google_ad_width = 170;
						google_ad_height = 200;
						//-->
						</script>
						<script type='text/javascript'
						src='http://pagead2.googlesyndication.com/pagead/show_ads.js'>
						</script>".
						"</figure>".
						"<p class='hover'>".
						"</p>".
						"</div>".
						"</article>";
					}/*else{
						//post eco banner
						$content .= "<article class='inline box post'>".
						"<div class='screenshot vintage'>".
						"<figure>".
						"<img src='http://www.greenpeace.org/argentina/Global/argentina/graphics/probono/artico/250x250.jpg' width='140' height='210' border='0' />".
						"</figure>".
						"<p class='hover'>".
						"<a class='hoverButton inline' target='_blank' href='http://www.greenpeace.org.ar/cyberacciones/index.php?cyberaccion=5122&referer=baners_webprobono_artico'>Mas</a>".
						"</p>".
						"</div>".
						"<div class='figcaption'>".
						"<p>Campa&ntilde;a Greenpeace</p>".
						"</div>".
						"</article>";
					}*/
				//}
				$counter ++;
			}
			$content .= "</div>";
			return $content;
		}	
}
?>
